package com.example.gok_mall.util;

import com.example.gok_mall.reponseData.RedisShardePool;
import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.ShardedJedis;

/**
 * @author Chakid
 * @since 2018-10-30 21:43
 * Redis api 工具类
 */
@Slf4j
public class RedisShardePoolUtil {


    //redis的expire操作 设置已存在key的有效期 单位是秒
    public static Long expire(String key,int exTime){
        ShardedJedis jedis =null;
        Long result =null;
        try{

            jedis= RedisShardePool.getJedis();
            result =jedis.expire(key,exTime);
        }
        catch (Exception e){
            log.error("expire key: {}  error",key,e);
            RedisShardePool.returnBrokenResource(jedis);
            return result;
        }
        RedisShardePool.returnResource(jedis);
        return result;
    }


    //redis的setEx操作 设置key的有效期 单位是秒
    public static String setEx(String key, String value,int exTime){
        ShardedJedis jedis =null;
        String result =null;
        try{

            jedis= RedisShardePool.getJedis();
            result =jedis.setex(key,exTime,value);
        }
        catch (Exception e){
            log.error("setex key: {} value: {} error",key,value,e);
            RedisShardePool.returnBrokenResource(jedis);
            return result;
        }
        RedisShardePool.returnResource(jedis);
        return result;
    }


    //redis的set操作
    public static String set(String key, String value){
        ShardedJedis jedis =null;
        String result =null;
       try{

           jedis= RedisShardePool.getJedis();
           result =jedis.set(key,value);
       }
        catch (Exception e){
           log.error("set key: {} value: {} error",key,value,e);
            RedisShardePool.returnBrokenResource(jedis);
           return result;
        }
        RedisShardePool.returnResource(jedis);
       return result;
    }


    //redis的get操作
    public static String get(String key){
        ShardedJedis jedis =null;
        String result =null;
        try{

            jedis= RedisShardePool.getJedis();
            result =jedis.get(key);
        }
        catch (Exception e){
            log.error("get key: {} value: {} error",key,result,e);
            RedisShardePool.returnBrokenResource(jedis);
            return result;
        }
        RedisShardePool.returnResource(jedis);
        return result;
    }

    //redis的删除操作
    public static Long del(String key){
        ShardedJedis jedis =null;
        Long result =null;
        try{

            jedis= RedisShardePool.getJedis();
            result =jedis.del(key);
        }
        catch (Exception e){
            log.error("del key: {}  error",key,e);
            RedisShardePool.returnBrokenResource(jedis);
            return result;
        }
        RedisShardePool.returnResource(jedis);
        return result;
    }


//测试上面的API
    public static void main(String[] args) {
        ShardedJedis jedis =RedisShardePool.getJedis();

        //set测试
        RedisShardePoolUtil.set("SetKeyTest","SetValue");


        //get测试
        String value= RedisShardePoolUtil.get("SetKeyTest");

        //有效期设置测试
        RedisShardePoolUtil.setEx("keyEx","KeyValue",60*10);

        //给已存在的key设置有效期
        RedisShardePoolUtil.expire("SetKeyTest",60*20);

        //删除key测试
        RedisShardePoolUtil.del("SetKeyTest");
    }

}
